/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.registration.web.servlet;

/**
 *
 * @author Usuario @date Oct 19, 2012
 *
 */
import com.registration.RegistrationManager;
import com.registration.core.Attendee;
import com.registration.reports.LabelToPdf;
import com.registration.reports.PreInscriptionToPdf;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author Polivares
 * @version
 */
public class PrintServlet extends HttpServlet {

    private RegistrationManager manager;
    private ApplicationContext appContext;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (appContext == null) {
            appContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            manager = (RegistrationManager) appContext.getBean("registrationManager");

        }

        Long id = 0L;
        String myType = request.getParameter("type");

        if (myType.equals("receipt")) {
            response.setContentType("application/pdf");

            id = Long.valueOf(request.getParameter("id"));
            Attendee attendee = manager.loadAttendee(id);

            PreInscriptionToPdf pdf = new PreInscriptionToPdf();
            pdf.write(attendee, response.getOutputStream());
            response.getOutputStream().close();
        }
        
        if (myType.equals("carnet")) {
            response.setContentType("application/pdf");

            id = Long.valueOf(request.getParameter("id"));
            Attendee attendee = manager.loadAttendee(id);

            LabelToPdf pdf = new LabelToPdf();
            pdf.write(attendee, response.getOutputStream());
            response.getOutputStream().close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
